/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.model.dbmanager;


import idea.ilogin.Installer;
import idea.ilogin.User;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;


/**
 * builds a JPA EntityManager _once_ per new successfully logged-in user. If
 * back-door, builds an EM with the default user, which is set up in
 * QnrLibrary/META-INF/persistence.xml.
 *
 * @author rejun001
 */
public class EntityManBuilder
{

    /**
     * Entity Manager used by JPA Persistence to handle all queries and Java
     * entity classes
     */
    private EntityManager em = null;
    /**
     * JPA Persistence Unit Id referring to a set of Entity classes (DB table
     * representations)
     */
    private String persistenceUnit = "QnrLibraryPU";
    /**
     * JPA database connection URL. Can be passed into a Map to modify login
     * info.
     */
    private final String persistenceUrl = "jdbc.postgresql://npc-qgdb1.npc.census.gov:5432/";
    /* implementation flag: 
     * if true,     builds the EntityManager using a Map of the login data.
     * else         just uses the existing Entity Manager.
     */
    private boolean isFirst = true;


    /**
     * Class Constructor. If there has been a login, uses the login info to
     * build a new EntityManager, Else uses the default EntityManager Factory
     */
    public EntityManBuilder()
    {
        User u = Installer.getLoggedUser(); // pick up the User from the login, if available
        if ( u != null )
        {
            em = build( u );
        }
        else
        {
            em = Persistence.createEntityManagerFactory( persistenceUnit ).createEntityManager(); // use the default
        }
    }


    /**
     * singleton. This is called whenever an EntityManager is needed by
     * DBManager.
     *
     * @return EntityManager
     */
    public EntityManager getEntityManager()
    {
        return em;
    }


    /**
     * builds an entity manager with the passed in authentication data
     *
     * @param user. This is created at LDAP login time.
     * @return EntityManager
     */
    private EntityManager build( User user )
    {
        if ( isFirst ) // we don't want to have to build it every time.
        {
            EntityManager myEm;
            String userId = user.getId();
            String pw = userId + "2012";  // obviously a temporary kludge due to current Postgres setup

            Map<String , String> map = new HashMap<>();
            map.put( "javax.persistence.jdbc.user" , userId );
            map.put( "javax.persistence.jdbc.password" , pw );
            myEm = Persistence.createEntityManagerFactory( persistenceUnit , map ).createEntityManager();
            isFirst = false;
            return myEm;  // We don't make this the class instance because other users may log in at the same time.
        }
        else
        {
            return em;
        }
    }

}
